package org.dizena.common.db;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.dizena.common.pojo.PageReq;
import org.dizena.utils.ObjUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
public class ByPageQuery<T> implements Serializable
{
    private long skip;
    private int limit;
    private String sortField;

    private String direction;
    private String searchField;
    private String searchContent;

    private Map<String, Object> param;
    private T vo;

    public ByPageQuery(PageReq req)
    {
        this.skip = req.getSkip();
        this.limit = req.getLimit();
        this.sortField = req.getSort();

        this.direction = req.getDire();
        this.searchField = req.getSearchField();
        this.searchContent = req.getSearchContent();
    }

    public void put(String key, Object value)
    {
        if (this.param == null)
        {
            this.param = new HashMap<>();
        }
        this.param.put(key, value);
    }

    public Map<String, Object> getParam()
    {
        if (param == null)
        {
            param = new HashMap<>();
        }
        if (vo != null)
        {
            param.putAll(ObjUtil.beanAttrToMapKV(vo));
        }
        if (StringUtils.isNotEmpty(searchField) && StringUtils.isNotEmpty(searchContent))
        {
            param.put(searchField, searchContent);
        }
        return param;
    }
}
